//
//  ViewController.m
//  录制中添加、更换滤镜
//
//  Created by 聚点互动iOS on 2020/12/29.
//

#import "ViewController.h"
#import "GGVideoManager.h"
#import "GGFilterHandler.h"
#import "GGFilterManager.h"

#import <Photos/Photos.h>

@interface ViewController ()<GGVideoManagerProtocol>
@property (nonatomic, strong) UILabel *label;
// 默认-1
@property (nonatomic, assign) NSInteger index;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    GPUImageView *displayView = [GPUImageView new];
    displayView.fillMode = kGPUImageFillModePreserveAspectRatio;
    displayView.frame = self.view.bounds;
    [self.view addSubview:displayView];
    
    [GGVideoManager manager].maxTime = 100;
    [GGVideoManager manager].delegate = self;
    [[GGVideoManager manager] addOutputView:displayView];
    [[GGVideoManager manager] startCapturing];
    
    [self addFuntionBtns];
    
    UILabel *label = [UILabel new];
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"等待录制中...";
    self.label = label;
    
    [self.view addSubview:self.label];
    self.label.frame = CGRectMake(0, 0, 375, 100);
    
}
#pragma mark - GGVideoManagerProtocol
/// 开始录制
/// @param manager manager description
- (void)didStartRecordVideoWithVideoManager:(GGVideoManager *)manager{
    NSLog(@"%s",__func__);
    self.label.text = @"录制中...";
}


/// 结束录制
/// @param manager manager description
/// @param totalTime 时长秒
/// @param filePath 文件路径
- (void)didEndRecordVideoWithVideoManager:(GGVideoManager *)manager time:(CGFloat)totalTime outputFile:(NSString *)filePath{
    NSLog(@"%s %@",__func__,filePath);
    self.label.text = @"结束录制了";
    
    //保存图片到【相机胶卷】
    /// 异步执行修改操作
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:filePath]];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {
                self.label.text = @"保存失败";
                NSLog(@"%@",@"保存失败");
            } else {
                NSLog(@"%@",@"保存成功");
                self.label.text = @"保存成功";
            }
        });
    }];
}



#pragma mark - ****
- (void)addFuntionBtns{
    NSArray<NSString *> *names = @[
        @"开始录制",
        @"结束录制",
        @"添加滤镜",
        @"更换滤镜",
    ];
    
    [names enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         
        UIButton *btn = [UIButton new];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        btn.tag = idx;
        btn.backgroundColor = [UIColor greenColor];
        btn.frame = CGRectMake(0, 100 + idx * 40, 100, 30);
        [btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
}

- (void)btnDidClicked:(UIButton *)sender{
    if (sender.tag == 0) {
        [[GGVideoManager manager] startRecording];
    }else if(sender.tag == 1){
        [[GGVideoManager manager] endRecording];
    }else if(sender.tag == 2){
        self.index++;
        if (self.index >= [GGFilterManager manager].defaults.count - 1) {
            self.index = 0;
        }
        GGFilterMaterialModel *model = [GGFilterManager manager].defaults[self.index];
        
        [[GGVideoManager manager].filterHandler setEffectFilter:[[GGFilterManager manager] filterWithFilterID:model.filter_id]];
    }else if(sender.tag == 3){
        self.index++;
        if (self.index >= [GGFilterManager manager].defaults.count - 1) {
            self.index = 0;
        }
        
        GGFilterMaterialModel *model = [GGFilterManager manager].defaults[self.index];
        
        [[GGVideoManager manager].filterHandler replaceLastFilter:[[GGFilterManager manager] filterWithFilterID:model.filter_id]];
    }
}


@end
